package android.pot.com.smartcar.ui.home.activity;

import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.util.ImageLoadUtils;
import android.pot.com.smartcar.widget.cycleview.ImageCycleScaleView;
import android.pot.com.smartcar.widget.cycleview.ImageCycleScaleView.ImageCycleViewListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaoshilu on 2017/7/27.
 */

public class PhotosScanActivity extends SwipeBackActivity implements View.OnClickListener {

    private ImageView back;

    private ImageCycleScaleView icv;

    private TextView tv_count_single;

    private ArrayList<String> imageUrls;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photos_scan);
        initView();
        initData();
        addListener();

    }

    @Override
    protected void initView() {
        back = (ImageView) findViewById(R.id.img_back);
        icv = (ImageCycleScaleView)findViewById(R.id.view_image_cycle);
        tv_count_single = (TextView) findViewById(R.id.tv_count_single);


    }

    @Override
    protected void addListener() {
        back.setOnClickListener(this);
        icv.setOnMaxListener(new ImageCycleScaleView.IsMaxListener() {
            @Override
            public void setMax(boolean isMax) {

            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                scrollToFinishActivity();
                break;


        }
    }


    private ImageCycleViewListener mImageCycleViewListener = new ImageCycleViewListener() {

        @Override
        public void onImageClick(int position, View imageView) {


        }


        @Override
        public void getPosition(int position) {
            tv_count_single.setText(position+1+"/"+imageUrls.size());
        }

        @Override
        public void displayImage(String imageURL, ImageView imageView) {
            // 此处使用了ImageLoader对图片进行加载！
            ImageLoadUtils.INSTANCE.loadImageView(imageView,imageURL);

        }
    };


    @Override
    protected void initData() {
        if (getIntent().getExtras()!=null){
            Bundle bundle = getIntent().getExtras();


            int curpage = (int)bundle.get("CurPic");
            boolean isSingle = bundle.getBoolean("single");
            if (bundle.getStringArrayList("img")!=null){
                List<String> mList = bundle.getStringArrayList("img");
                imageUrls = new ArrayList<String>();
                if (isSingle){
                    imageUrls.add(mList.get(curpage));
                    icv.setImageResources(imageUrls, mImageCycleViewListener, false, false);
                    icv.setCurImg(0);
                    tv_count_single.setVisibility(View.GONE);
                }else {
                    for (int i = 0; i < mList.size(); i++) {
                        imageUrls.add(mList.get(i));
                    }
                    icv.setImageResources(imageUrls, mImageCycleViewListener, false, false);
                    icv.setCurImg(curpage);
                    tv_count_single.setVisibility(View.VISIBLE);
                    tv_count_single.setText(curpage+1+"/"+imageUrls.size());
                }


            }
        }

    }


}